package cn.iocoder.yudao.module.crossborder.platform.controller.admin.platformstore;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.crossborder.platform.controller.admin.platformstore.vo.*;
import cn.iocoder.yudao.module.crossborder.platform.dal.dataobject.platformstore.PlatformStoreDO;
import cn.iocoder.yudao.module.crossborder.platform.service.platformstore.PlatformStoreService;

@Tag(name = "管理后台 - 跨境电商店铺管理")
@RestController
@RequestMapping("/crossborder/platform-store")
@Validated
public class PlatformStoreController {

    @Resource
    private PlatformStoreService platformStoreService;

    @PostMapping("/create")
    @Operation(summary = "创建跨境电商店铺管理")
    @PreAuthorize("@ss.hasPermission('crossborder:platform-store:create')")
    public CommonResult<Long> createPlatformStore(@Valid @RequestBody PlatformStoreSaveReqVO createReqVO) {
        return success(platformStoreService.createPlatformStore(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新跨境电商店铺管理")
    @PreAuthorize("@ss.hasPermission('crossborder:platform-store:update')")
    public CommonResult<Boolean> updatePlatformStore(@Valid @RequestBody PlatformStoreSaveReqVO updateReqVO) {
        platformStoreService.updatePlatformStore(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除跨境电商店铺管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('crossborder:platform-store:delete')")
    public CommonResult<Boolean> deletePlatformStore(@RequestParam("id") Long id) {
        platformStoreService.deletePlatformStore(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除跨境电商店铺管理")
                @PreAuthorize("@ss.hasPermission('crossborder:platform-store:delete')")
    public CommonResult<Boolean> deletePlatformStoreList(@RequestParam("ids") List<Long> ids) {
        platformStoreService.deletePlatformStoreListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得跨境电商店铺管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crossborder:platform-store:query')")
    public CommonResult<PlatformStoreRespVO> getPlatformStore(@RequestParam("id") Long id) {
        PlatformStoreDO platformStore = platformStoreService.getPlatformStore(id);
        return success(BeanUtils.toBean(platformStore, PlatformStoreRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得跨境电商店铺管理分页")
    @PreAuthorize("@ss.hasPermission('crossborder:platform-store:query')")
    public CommonResult<PageResult<PlatformStoreRespVO>> getPlatformStorePage(@Valid PlatformStorePageReqVO pageReqVO) {
        PageResult<PlatformStoreDO> pageResult = platformStoreService.getPlatformStorePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PlatformStoreRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出跨境电商店铺管理 Excel")
    @PreAuthorize("@ss.hasPermission('crossborder:platform-store:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPlatformStoreExcel(@Valid PlatformStorePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PlatformStoreDO> list = platformStoreService.getPlatformStorePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "跨境电商店铺管理.xls", "数据", PlatformStoreRespVO.class,
                        BeanUtils.toBean(list, PlatformStoreRespVO.class));
    }

}